<template>
  <div class="campus-level-tooltip" v-show="visible">
    <div class="feature-container">
      <div class="content-scroll">
        <div class="title">院校层次<span>（多选）</span></div>

        <div class="feature-box-feature">
          <div :class="{
              'province-item': true,
              active: item.checked,
            }" v-for="(item, index) in schoolFeatureList" :key="item.name"
            @click="chooseSchoolFeature(item, 'schoolFeature')">
            {{ item.name }}
          </div>
        </div>
        <div class="title">办学类型<span>（多选）</span></div>
        <div class="feature-box-feature">
          <div :class="{
              'province-item': true,
              active: item.checked,
            }" v-for="(item, index) in schoolNatureList" :key="item.name"
            @click="chooseSchoolFeature(item, 'schoolNature')">
            {{ item.name }}
          </div>
        </div>
        <div class="title">办学层次<span>（多选）</span></div>
        <div class="feature-box-feature">
          <div :class="{
              'province-item': true,
              active: item.checked,
            }" v-for="(item, index) in schoolLevelList" :key="item.name"
            @click="chooseSchoolFeature(item, 'schoolLevel')">
            {{ item.name }}
          </div>
        </div>
        <div class="title">院校类型<span>（多选）</span></div>
        <div class="feature-box-feature">
          <div :class="{
              'province-item': true,
              active: item.checked,
            }" v-for="(item, index) in schoolTypeList" :key="item.name"
            @click="chooseSchoolFeature(item, 'schoolType')">
            {{ item.name }}
          </div>
        </div>
      </div>

      <div class="bottom-btn">
        <div :class="['left-btn', activeButton ? 'active' : '']" @click="clear">
          清除已选类型
        </div>
        <div class="right-btn" @click="confirm">确 定</div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from "vue";
import schoolFeatureData from "@/utils/schoolFeatureData";
import schoolNatureData from "@/utils/schoolNatureData";
import schoolLevelData from "@/utils/schoolLevelData";
import schoolTypeData from "@/utils/schoolTypeData";

const emit = defineEmits(["confirmSchoolFeature", "close"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: false,
  },
});

const schoolFeatureList = ref([]);
const schoolNatureList = ref([]);
const schoolLevelList = ref([]);
const schoolTypeList = ref([]);

const getData = (list) => {
  return list.map((item) => ({
    ...item,
    checked: item.name === "不限", // 默认选中"不限"选项
  }));
};

const getList = () => {
  schoolFeatureList.value = getData(schoolFeatureData);
  schoolNatureList.value = getData(schoolNatureData);
  schoolLevelList.value = getData(schoolLevelData);
  schoolTypeList.value = getData(schoolTypeData);
};

getList();

const chooseSchoolFeature = (item, flag) => {
  item.checked = !item.checked;
  if (flag == "schoolFeature") {
    // 点击了不限
    if (!item.code) {
      schoolFeatureList.value.forEach((item) => {
        item.checked = false;
      });
      item.checked = true;
    } else {
      // 点击了其他类型,重置不限
      schoolFeatureList.value[0].checked = false;
    }
  }
  if (flag == "schoolNature") {
    // 点击了不限
    if (!item.code) {
      schoolNatureList.value.forEach((item) => {
        item.checked = false;
      });
      item.checked = true;
    } else {
      // 点击了其他类型,重置不限
      schoolNatureList.value[0].checked = false;
    }
  }
  if (flag == "schoolType") {
    // 点击了不限
    if (!item.code) {
      schoolTypeList.value.forEach((item) => {
        item.checked = false;
      });
      item.checked = true;
    } else {
      // 点击了其他类型,重置不限
      schoolTypeList.value[0].checked = false;
    }
  }
  getSelectData();
};

const clear = () => {
  schoolFeatureList.value.forEach((item) => {
    item.checked = item.name === "不限"; // 默认选中"不限"选项
  });
  schoolNatureList.value.forEach((item) => {
    item.checked = item.name === "不限"; // 默认选中"不限"选项
  });
  schoolLevelList.value.forEach((item) => {
    item.checked = false;
  });
  schoolTypeList.value.forEach((item) => {
    item.checked = item.name === "不限"; // 默认选中"不限"选项
  });
  getSelectData();
};

const activeButton = ref(false);
let selectObj = {};

const getSelectData = () => {
  const chosenSchoolFeatureList = schoolFeatureList.value.filter(
    (item) => item.checked
  );
  const chosenSchoolNatureList = schoolNatureList.value.filter(
    (item) => item.checked
  );
  const chosenSchoolLevelList = schoolLevelList.value.filter(
    (item) => item.checked
  );
  const chosenSchoolTypeList = schoolTypeList.value.filter(
    (item) => item.checked
  );
  selectObj = {
    schoolFeatureList:
      chosenSchoolFeatureList.length > 0
        ? chosenSchoolFeatureList
        : [schoolFeatureList.value[0]],
    schoolNatureList:
      chosenSchoolNatureList.length > 0
        ? chosenSchoolNatureList
        : [schoolNatureList.value[0]],
    schoolLevelList: chosenSchoolLevelList,
    schoolTypeList:
      chosenSchoolTypeList.length > 0
        ? chosenSchoolTypeList
        : [schoolTypeList.value[0]],
  };
  activeButton.value =
    (chosenSchoolFeatureList.length > 0 &&
      chosenSchoolFeatureList[0].name !== "不限") ||
    chosenSchoolNatureList.length > 0 ||
    chosenSchoolNatureList.length > 0;
};

getSelectData();

const confirm = () => {
  emit("confirmSchoolFeature", selectObj);
  emit("close");
};

onMounted(() => {});
</script>

<style scoped lang="scss">
.campus-level-tooltip {
  position: absolute;
  top: calc(100% + 10px);
  left: 110px;
  z-index: 1000;
  background: #ffffff;
  border-radius: 8px;
  box-shadow: 0px 2px 28px 6px rgba(90, 122, 162, 0.3);
  border: 1px solid #e4e7ed;
  width: 1060px;
  max-height: 600px;
  overflow: hidden;
}

.feature-container {
  .content-scroll {
    height: 344px;
    overflow-y: auto;
    padding: 6px 16px 150px 32px;
    .title {
      padding: 32px 0 24px 0;
      display: flex;
      align-items: flex-end;
      font-family: PingFang-SC, PingFang-SC;
      font-weight: bold;
      font-size: 28px;
      color: #222222;
      line-height: 22px;
      text-align: left;
      font-style: normal;
      span {
        font-size: 22px;
        color: #999999;
      }
    }
    .feature-box-feature {
      display: grid;
      grid-template-columns: repeat(3, 1fr);
      gap: 20px;
      padding: 25px 0;
      .province-item {
        flex-shrink: 0;
        display: flex;
        align-items: center;
        justify-content: center;
        border-radius: 8px;
        font-family: "PingFang-SC, PingFang-SC";
        font-size: 28px;
        padding: 12px 20px;
        color: #333333;
        background: #f8f8f8;
        border: #ffffff 1px solid;
        cursor: pointer;
        transition: all 0.3s ease;
        box-sizing: border-box;
        border: transparent 3px solid;
        &.active {
          background: #e5edff;
          border: #0b6eff 3px solid;
          color: #0b6eff;
          font-weight: bold;
        }
      }
    }
  }
  .bottom-btn {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    padding: 30px;
    background: #ffffff;
    box-shadow: 0px -2px 4px 0px rgba(188, 188, 188, 0.5);
    box-sizing: border-box;
    display: flex;
    flex-direction: row;
    justify-content: center;
    gap: 24px;
    z-index: 2;

    .left-btn,
    .right-btn {
      flex-shrink: 0;
      width: 296px;
      height: 60px;
      border-radius: 12px;
      font-family: PingFang-SC, PingFang-SC;
      font-weight: bold;
      font-size: 24px;
      color: #999999;
      display: flex;
      flex-direction: row;
      align-items: center;
      justify-content: center;
      cursor: pointer;
      transition: all 0.3s ease;
    }

    .left-btn {
      background: #c1c1c1;
      color: #ffffff;
      // border: 1px solid #f8f8f8;
      &.active {
        // border: 1px solid #0b6eff;
        // color: #0b6eff;
      }
    }
    .right-btn {
      color: #ffffff;
      background: #0b6eff;

      .confirm {
        margin-left: 10px;
      }
    }
  }
}
</style> 